package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.WarehouseLocationMapper;
import com.zhijian.warehouse.manage.entity.dto.UserDto;
import com.zhijian.warehouse.manage.entity.po.Warehouse;
import com.zhijian.warehouse.manage.entity.po.WarehouseLocation;
import com.zhijian.warehouse.manage.entity.vo.request.WarehouseLocationSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.WarehouseLocationSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.WarehouseLocationVo;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.enums.StatusEnum;
import com.zhijian.warehouse.manage.holder.CurrentWebUserHolder;
import com.zhijian.warehouse.manage.service.WarehouseLocationService;
import com.zhijian.warehouse.manage.service.WarehouseService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 仓库库位业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-10 19:40:29
 */
@Slf4j
@Service
public class WarehouseLocationServiceImpl implements WarehouseLocationService {

    @Autowired
    private WarehouseLocationMapper mapper;

    @Autowired
    private WarehouseService warehouseService;

    @Override
    public WarehouseLocation selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public WarehouseLocationVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        WarehouseLocation warehouseLocation = this.selectById(id);
        AssertCommonUtil.notNull(warehouseLocation, "此数据不存在或者已被删除");
        AssertCommonUtil.check(Objects.equals(Integer.valueOf(warehouseLocation.getStatus()), StatusEnum.NORMAL.getCode()), "启用状态无法删除");
        this.updateByPrimaryKeySelective(WarehouseLocation.builder().id(id).isDelete(DelFlagEnum.DELETED.value).build());
        // mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            for (Long l : idList) {
                this.deleteById(l);
            }
        }
    }

    @Override
    public Long insert(WarehouseLocation warehouseLocation) {
        Date now = new Date();
        warehouseLocation.setCreateTime(now);
        warehouseLocation.setUpdateTime(now);
        return mapper.insert(warehouseLocation) > 0 ? warehouseLocation.getId() : 0L;
    }

    @Override
    public Long insertSelective(WarehouseLocation warehouseLocation) {
        Date now = new Date();
        warehouseLocation.setCreateTime(now);
        warehouseLocation.setUpdateTime(now);
        return mapper.insertSelective(warehouseLocation) > 0 ? warehouseLocation.getId() : 0L;
    }

    @Override
    public boolean update(WarehouseLocation warehouseLocation) {
        Date now = new Date();
        warehouseLocation.setUpdateTime(now);
        return mapper.updateByPrimaryKey(warehouseLocation) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(WarehouseLocation record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(WarehouseLocationSaveOrUpdateVo saveOrUpdateVo) {
        Warehouse warehouse = warehouseService.selectById(saveOrUpdateVo.getWarehouseId());
        AssertCommonUtil.notNull(warehouse, "此仓库不存在或者已被删除");
        Integer count = this.count(WarehouseLocationSearchVo.builder()
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        count = this.count(WarehouseLocationSearchVo.builder()
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .nameAccurate(saveOrUpdateVo.getName())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        WarehouseLocation warehouseLocation = new WarehouseLocation();
        BeanUtils.copyProperties(saveOrUpdateVo, warehouseLocation);
        boolean b = this.insertSelective(warehouseLocation) != 0;
        saveOrUpdateVo.setId(warehouseLocation.getId());
        return  b;
    }

    @Override
    public boolean modify(WarehouseLocationSaveOrUpdateVo saveOrUpdateVo) {
        WarehouseLocation old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        Warehouse warehouse = warehouseService.selectById(saveOrUpdateVo.getWarehouseId());
        AssertCommonUtil.notNull(warehouse, "此仓库不存在或者已被删除");
        Integer count = this.count(WarehouseLocationSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .number(saveOrUpdateVo.getNumber())
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        count = this.count(WarehouseLocationSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .nameAccurate(saveOrUpdateVo.getName())
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        old.setName(saveOrUpdateVo.getName());
        old.setNumber(saveOrUpdateVo.getNumber());
        old.setStatus(saveOrUpdateVo.getStatus());
        old.setNote(saveOrUpdateVo.getNote());
        old.setUpdateUserId(saveOrUpdateVo.getUpdateUserId());
        old.setUpdateUserName(saveOrUpdateVo.getUpdateUserName());
        old.setIsDelete(DelFlagEnum.USING.value);
        return this.update(old);
    }

    @Override
    public WarehouseLocationVo selectOne(WarehouseLocationSearchVo warehouseLocationSearchVo) {
        return mapper.selectOne(warehouseLocationSearchVo);
    }

    @Override
    public Integer count(WarehouseLocationSearchVo warehouseLocationSearchVo) {
        return mapper.count(warehouseLocationSearchVo);
    }

    @Override
    public List<WarehouseLocationVo> selectList(WarehouseLocationSearchVo warehouseLocationSearchVo) {
        return mapper.selectList(warehouseLocationSearchVo);
    }

    @Override
    public Page<WarehouseLocationVo> selectPage(WarehouseLocationSearchVo warehouseLocationSearchVo) {
        AssertCommonUtil.check(Objects.isNull(warehouseLocationSearchVo.getPageNum()) || Objects.isNull(warehouseLocationSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(warehouseLocationSearchVo);
        //分页
        Page<WarehouseLocationVo> page = new Page<>(true, count, warehouseLocationSearchVo.getPageNum(), warehouseLocationSearchVo.getPageSize());
        warehouseLocationSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<WarehouseLocationVo> responseVos = this.selectList(warehouseLocationSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<WarehouseLocation> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void changeStatus(Long id, Byte status) {
        WarehouseLocation old = this.selectById(id);
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        old.setStatus(status);
        UserDto user = CurrentWebUserHolder.getUser();
        old.setUpdateUserId(user.getId());
        old.setUpdateUserName(user.getName());
        this.update(old);
    }

    @Override
    public boolean bindingBarcode(WarehouseLocationSaveOrUpdateVo param) {
        AssertCommonUtil.check(BeanUtil.isEmpty(param.getId()), "库位id不能为空");
        AssertCommonUtil.check(BeanUtil.isEmpty(param.getBarCodeStatus()), "操作类型不能为空");
        if (param.getBarCodeStatus() == 1) {
            AssertCommonUtil.check(BeanUtil.isEmpty(param.getBarCode()), "条码不能为空");
            Integer count = this.count(WarehouseLocationSearchVo.builder()
                    .notId(param.getId())
                    .barCode(param.getBarCode())
                    .isDelete(DelFlagEnum.USING.value)
                    .build());
            AssertCommonUtil.check(count > 0, "该条码已被其他库位绑定");
            return this.updateByPrimaryKeySelective(WarehouseLocation.builder().id(param.getId())
                            .barCodeStatus(BeanUtil.isEmpty(param.getBarCode())?0:1)
                    .barCode(param.getBarCode() == null ? " " : param.getBarCode()).build());
        } else {
            mapper.updateBarCodeNull(param.getId());
        }
        return true;
    }


    @Override
    public WarehouseLocationVo selectByBarCode(WarehouseLocationSearchVo param) {
        return selectOne(WarehouseLocationSearchVo.builder().
                isDelete(DelFlagEnum.USING.value).
                barCode(param.getBarCode()).build());
    }
}
